﻿using System;
using System.Collections.ObjectModel;
using System.ComponentModel;

using GroupChallenge.Web;

namespace GroupChallenge
{
    public class GameViewModel : INotifyPropertyChanged
    {
        #region Properties

        public int? HighScore { get; set; }

        private ObservableCollection<Question> _RandomizedQuestions = new ObservableCollection<Question>();
        public ObservableCollection<Question> RandomizedQuestions
        {
            get { return _RandomizedQuestions; }
            set
            {
                _RandomizedQuestions = value;

                RaisePropertyChanged("RandomizedQuestions");
            }
        }

        private Question _CurrentQuestion = null;
        public Question CurrentQuestion
        {
            get { return _CurrentQuestion; }
            set
            {
                if (value != _CurrentQuestion)
                {
                    _CurrentQuestion = value;

                    RaisePropertyChanged("CurrentQuestion");
                }
            }
        }

        private int _CurrentQuestionId = 0;
        public int CurrentQuestionId
        {
            get { return _CurrentQuestionId; }
            set
            {
                if (value != _CurrentQuestionId)
                {
                    _CurrentQuestionId = value;

                    RaisePropertyChanged("CurrentQuestionId");
                }
            }
        }
        private int _CurrentQuestionIndex = 0;
        public int CurrentQuestionIndex
        {
            get { return _CurrentQuestionIndex; }
            set
            {
                if (value != _CurrentQuestionIndex)
                {
                    _CurrentQuestionIndex = value;

                    RaisePropertyChanged("CurrentQuestionIndex");
                }
            }
        }

        private DateTime _LastChangeTime;
        public DateTime LastChangeTime
        {
            get { return _LastChangeTime; }
            set
            {
                if (value != _LastChangeTime)
                {
                    _LastChangeTime = value;

                    RaisePropertyChanged("LastChangeTime");
                }
            }
        }

        private int _WonByPlayerId = 0;
        public int WonByPlayerId
        {
            get { return _WonByPlayerId; }
            set
            {
                _WonByPlayerId = value;

                RaisePropertyChanged("WonByPlayerId");
            }
        }

        private string _WonByPlayerNickName = string.Empty;
        public string WonByPlayerNickName
        {
            get { return _WonByPlayerNickName; }
            set
            {
                if (value != _WonByPlayerNickName)
                {
                    _WonByPlayerNickName = value;

                    RaisePropertyChanged("WonByPlayerNickName");
                }
            }
        }

        private string _WinnerMessage = string.Empty;
        public string WinnerMessage
        {
            get { return _WinnerMessage; }
            set
            {
                if (value != _WinnerMessage)
                {
                    _WinnerMessage = value;

                    RaisePropertyChanged("WinnerMessage");
                }
            }
        }

        #endregion                              // Properties

        #region NotifyPropertyChanged

        public event PropertyChangedEventHandler PropertyChanged;
        private void RaisePropertyChanged(String propertyName)
        {
            if (null != PropertyChanged)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        #endregion                              // NotifyPropertyChanged

        internal bool NextQuestion()
        {
            bool ret = true;

            if (MainApp.CurrentGame.CurrentQuestionIndex < 0)
                CurrentQuestionIndex = 0;
            else
                CurrentQuestionIndex++;

            if (MainApp.CurrentGame.CurrentQuestionIndex < MainApp.CurrentGame.RandomizedQuestions.Count)
                CurrentQuestion = RandomizedQuestions[CurrentQuestionIndex];
            else
                ret = false;

            return ret;
        }
    }
}
